<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_Request
{
	protected $_params = array();
	protected $_postParams = array();
	/**
	 * @var Gio_Core_Request
	 */
	private static $_instance = null; 
	
	/**
	 * @return Gio_Core_Request
	 */
	public static function getInstance()
	{
		if (null == self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function __construct()
	{		
		if (get_magic_quotes_gpc()) {
		    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
		    while (list($key, $val) = each($process)) {
		        foreach ($val as $k => $v) {
		            unset($process[$key][$k]);
		            if (is_array($v)) {
		                $process[$key][stripslashes($k)] = $v;
		                $process[] = &$process[$key][stripslashes($k)];
		            } else {
		                $process[$key][stripslashes($k)] = stripslashes($v);
		            }
		        }
		    }
		    unset($process);
		}
		$this->_params = $_GET;
		$this->_postParams = $_POST;
	}
	
	public function setParam($name, $value = null) {
		$string = new Gio_Core_String();
		$_GET[$name] = $string->escape($value);
		$this->_params[$name] = $string->escape($value);
	}
	
	public function getParam($name, $defaultValue = null)
	{
		$string = new Gio_Core_String();
		if (isset($this->_params[$name]) && $this->_params[$name] 
											&& $this->_params[$name] != 'GLOBAL') 
		{
			return $string->escape($this->_params[$name]);
		} elseif (isset($this->_postParams[$name])) {
			return $this->_postParams[$name];
		}
		return $defaultValue;
	}
	
	public function getParams()
	{
		return $this->_params;
	}
	
	public function getPostParams()
	{
		return $this->_postParams;
	}
	
	public function getPost($name)
	{
		if (isset($this->_postParams[$name])) {
			if (gettype($this->_postParams[$name]) == 'string') {
				return trim($this->_postParams[$name]);
			}
			return $this->_postParams[$name];
		}
		return null;
	}
	
	public function isPost($name = null)
	{
		if (null == $name) {
			if (count($this->_postParams) > 0) {
				return true;
			}
			return false;
		} else {
			if (isset($this->_postParams[$name])) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	public function getRequestUri()
	{
		return isset($_SERVER['REDIRECT_URL'])
					? $_SERVER['REDIRECT_URL'] : null;
//		return isset($_SERVER['REQUEST_URI'])
//					? $_SERVER['REQUEST_URI'] : null;
	}
	
	public function setRequestUri($uri)
	{
		$_SERVER['REQUEST_URI'] = $uri;
		return $this;
	}
	
	public function setRedirectUrl($url)
	{
		$_SERVER['REDIRECT_URL'] = $url;
		return $this;
	}
	
	public function getRedirectUrl()
	{
		return isset($_SERVER['REDIRECT_URL'])
					? $_SERVER['REDIRECT_URL'] : null;
	}
	
	public function getServerName()
	{
		return isset($_SERVER['SERVER_NAME'])
					? $_SERVER['SERVER_NAME'] : null;
	}
	
	public function getRemoteIp()
	{
		return isset($_SERVER['REMOTE_ADDR']) 
					? $_SERVER['REMOTE_ADDR'] : null;
	}
	
	public function getServerPort()
	{
		return isset($_SERVER['SERVER_PORT'])
					? $_SERVER['SERVER_PORT'] : null;
	}
	
	public function getServerProtocol()
	{
		return isset($_SERVER['SERVER_PROTOCOL'])
					? $_SERVER['SERVER_PROTOCOL'] : null;
	}
	
	public function getBaseUrl()
	{
		$server = $_SERVER;
		$serverName = $this->getServerName();
		$serverName = str_replace('www.', '', $serverName);
		$serverPort = $this->getServerPort();
		$serverPort = ($serverPort == 80) ? '' : ':' . $serverPort;
		$serverProtocol = $this->getServerProtocol();
		
		$scriptName = $server['SCRIPT_NAME'];
		$temp = explode('/', $scriptName);
		unset($temp[count($temp) - 1]);
		$redirectUrl = implode('/', $temp);
//		$redirectUrl = $this->getRedirectUrl();
//		$redirectUrl = rtrim(ltrim($redirectUrl, '/'), '/');
//		$redirectUrl = ($redirectUrl) ? '/' . $redirectUrl : null;
		
		$baseUrl = 'http://' . $serverName . $serverPort . $redirectUrl; 
		return $baseUrl; 
	}
}